<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所处物业" prop="pmid">
        <el-select v-model="queryParams.pmid" placeholder="请选择所处物业" clearable :style="{width: '100%'}" @change="getCarsHomeList">
          <el-option v-for="(item, index) in propertyapilist" :key="index" :label="item.propertyName" :value="item.pmid"
                     :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所处车场" prop="mcid">
        <el-select v-model="queryParams.mcid" placeholder="请选择所处车场所处车场" clearable :style="{width: '100%'}" >
          <el-option v-for="(item, index) in carshomeapilist" :key="index" :label="item.chname" :value="item.chid"
                     :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商家名称" prop="merchantName">
        <el-input
          v-model="queryParams.merchantName"
          placeholder="请输入商家名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:management:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:management:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:management:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="topup"
          v-hasPermi="['system:management:edit']"
        >充值
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:management:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="managementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="自增ID" align="center" prop="pmid"/>
      <el-table-column label="商家名称" align="center" prop="merchantName"/>
      <el-table-column label="商家用户" align="center" prop="user.username"/>
      <el-table-column label="商家所处车场" align="center" prop="carsHome.chname"/>
      <el-table-column label="商家余额" align="center" prop="balance" />
      <el-table-column label="可透支额度" align="center" prop="credit" />
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:management:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:management:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listManagement,
  getManagement,
  delManagement,
  addManagement,
  updateManagement,
  getCarsHomeApi,
  getPropertyApi
} from "@/api/modules/merchat/merchatm/merchat";

export default {
  name: "Management",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商家管理表格数据
      managementList: [],
      //操控车场集合的物业id
      pmid:'',
      //车场集合
      carshomeapilist:[],
      //物业集合
      propertyapilist:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        status: null,
        deleted: null,
        merchantName: null,
        muid: null,
        mcid: null,
        pmid: null,
      },
      // 表单校验
      rules: {
        code: [
          {required: true, message: "唯一编码,前台使用不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "状态不能为空", trigger: "change"}
        ],
        deleted: [
          {required: true, message: "是否删除不能为空", trigger: "blur"}
        ],
        createTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
        updateTime: [
          {required: true, message: "修改时间不能为空", trigger: "blur"}
        ],
        merchantName: [
          {required: true, message: "商家名称不能为空", trigger: "blur"}
        ],
        muid: [
          {required: true, message: "商家用户不能为空", trigger: "blur"}
        ],
        mcid: [
          {required: true, message: "商家所处车厂不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商家管理列表 */
    getList() {
      this.loading = true;
      listManagement(this.queryParams).then(response => {
        this.managementList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      getPropertyApi(this.queryParams).then(response=>{
        this.propertyapilist = response.rows;
      }).catch(err=>{
        alert("物业模块掉线")
      });
    },
    getCarsHomeList(){
      this.queryParams.mcid = null;
      getCarsHomeApi(this.queryParams).then(response=>{
        this.carshomeapilist = response.rows;
      }).catch(err=>{
        alert("车场模块掉线")
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
      this.carshomeapilist=[];
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.pmid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.title = "添加商家管理";
      this.$router.push({path:"addmerchant",query:{title:this.title}})
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.title = "修改商家管理";
      const pmid = row.pmid || this.ids
      getManagement(pmid).then(response => {
        this.$router.push({path:"addmerchant",query:{title:this.title,data:response.data}})
      });
    },
    topup(row) {
      const pmid = row.pmid || this.ids;
      getManagement(pmid).then(response => {
        this.$router.push({path:"topup",query:{data:response.data}})
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.pmid != null) {
            updateManagement(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addManagement(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const pmids = row.pmid || this.ids;
      this.$modal.confirm('是否确认删除商家管理编号为"' + pmids + '"的数据项？').then(function () {
        return delManagement(pmids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/management/export', {
        ...this.queryParams
      }, `management_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
